package main

import (
	"fmt"
	"heap"
	"sort"
)

/*
把数组中偶数移到左侧，奇数移到右侧
*/
//自定义排序法
func sortArrayByParity(nums []int) []int {
	sort.Slice(nums, func(i, j int) bool {
		isOddA := nums[i]%2 == 0
		isOddB := nums[j]%2 == 0
		if isOddA && !isOddB {
			return true
		}
		return false
	})
	return nums
}

//原地交换法
func sortArrayByParity2(nums []int) []int {
	mid := 0
	for i := 0; i < len(nums); i++ {
		if nums[i]%2 == 0 {
			nums[i], nums[mid] = nums[mid], nums[i]
			mid++
		}
	}
	return nums
}

func main() {
	nums := []int{2, 3, 1, 0, 6, 5, 8, 7, 9}
	n := len(nums)

	nums1 := make([]int, n)
	copy(nums1, nums)
	sortArrayByParity2(nums1)
	fmt.Println(nums1)

	nums2 := make([]int, n)
	copy(nums2, nums)
	heap.HeapSort(nums2)
	fmt.Println(nums2)
}
